﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuditModel;
using AuditWeb.Models;

namespace AuditWeb.Controllers
{
    [Authorize(Roles = "评委")]
    public class PWController : Controller
    {
        AuditDBEntities db = new AuditDBEntities();
        IAnnualEventBL iaebl = new AnnualEventBL();
        IScoreBL isbl = new ScoreBL();
        IExpertJudgeBL iejbl = new ExpertJudgeBL();
        IArchiveBL iabl = new ArchiveBL();

        [Authorize(Roles = "评委")]
        public ActionResult SetContactInfo()
        {
            ExpertJudge ej = db.ExpertJudges.SingleOrDefault(e => e.SerialNumber == User.Identity.Name);
            ExpertJudgeContactInfo model = new ExpertJudgeContactInfo()
            {
                ContactAddress=ej.ContactAddress,
                ZipCode=ej.ZipCode
            };
            return View(model);
        }

        [Authorize(Roles = "评委")]
        [HttpPost]
        public ActionResult SetContactInfo(ExpertJudgeContactInfo model)
        {
            if (ModelState.IsValid)
            {
                ExpertJudge ej = db.ExpertJudges.SingleOrDefault(e => e.SerialNumber == User.Identity.Name);
                ej.ContactAddress = model.ContactAddress;
                ej.ZipCode = model.ZipCode;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(model);
        }

        //
        // GET: /PW/
        [Authorize(Roles = "评委")]
        public ActionResult Index()
        {
            string serialnumber = User.Identity.Name;
            string eventid = iaebl.GetActiveEvents().FirstOrDefault().ID;
            ExpertJudge ej = db.ExpertJudges.SingleOrDefault(e => e.SerialNumber == User.Identity.Name);

            if (string.IsNullOrEmpty(ej.ContactAddress) && string.IsNullOrEmpty(ej.ZipCode))
            {
                return RedirectToAction("SetContactInfo", "PW");
            }

            int subjectid =ej.SubjectID.Value;
            ViewBag.ExpertJudgeName=ej.RealName;

            ViewBag.AnnualEventId = eventid;
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(eventid);
            ViewBag.SubjectName = db.Subjects.SingleOrDefault(s => s.Id == subjectid).Name;

            var archives = from a in db.Archives where a.SubjectID == subjectid select a;
            ViewBag.ArchivesCount = archives.Count();
            
            var scores = isbl.GetExpertScores(User.Identity.Name);
            ViewBag.ScoreCount = scores.Count();

            var ClassDates = (from a in archives select new PWCountyClassDate() { CountyName = a.County.Name, ClassDate = a.ClassDate.Value, CountyCode=a.CountyCode }).Distinct();
            ViewBag.ClassDateList = ClassDates.ToList();


            //iejbl.GetScore(serialnumber);
            return View();
        }

        [Authorize(Roles = "评委")]
        public ActionResult ArchiveDetails(string id)
        {
            ExpertJudge ej = db.ExpertJudges.SingleOrDefault(e => e.SerialNumber == User.Identity.Name);

            if (string.IsNullOrEmpty(ej.ContactAddress) && string.IsNullOrEmpty(ej.ZipCode))
            {
                return RedirectToAction("SetContactInfo", "PW");
            }

            Archive archive = db.Archives.Single(a => a.ID == id);
            string serialnumber = User.Identity.Name;
            string eventid = archive.AnnualEventID;
            int subjectid = (int)ej.SubjectID;

            ViewBag.AnnualEventId = eventid;
            ViewBag.AnnualEvent = archive.AnnualEvent;
            ViewBag.SubjectName = db.Subjects.SingleOrDefault(s => s.Id == subjectid).Name;
            return View(archive);
        }

        public ActionResult MyScores(string msg)
        {
            ExpertJudge ej = db.ExpertJudges.SingleOrDefault(e => e.SerialNumber == User.Identity.Name);

            if (string.IsNullOrEmpty(ej.ContactAddress) && string.IsNullOrEmpty(ej.ZipCode))
            {
                return RedirectToAction("SetContactInfo", "PW");
            }
            
            string eventid = iaebl.GetActiveEvents().FirstOrDefault().ID;
            int subjectid = (int)ej.SubjectID;

            ViewBag.AnnualEventId = eventid;
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(eventid);
            ViewBag.SubjectName = db.Subjects.SingleOrDefault(s => s.Id == subjectid).Name;
            ViewBag.ErrorMsg = string.IsNullOrEmpty(msg) ? "" : msg;

            return View(isbl.GetExpertScores(User.Identity.Name));
        }

        public ActionResult Archives(string id, string countycode, string archiveid)
        {
            ExpertJudge ej = db.ExpertJudges.SingleOrDefault(e => e.SerialNumber == User.Identity.Name);

            if (string.IsNullOrEmpty(ej.ContactAddress) && string.IsNullOrEmpty(ej.ZipCode))
            {
                return RedirectToAction("SetContactInfo", "PW");
            }

            string annualeventid = id;
            if (string.IsNullOrEmpty(id))
                annualeventid = iaebl.GetActiveEvents().FirstOrDefault().ID;

            countycode = countycode == null ? "" : countycode.Trim();
            int _subjectid = (int)ej.SubjectID;
            archiveid = archiveid == null ? "" : archiveid.Trim();
            AdminArchiveModel aam = new AdminArchiveModel() { AnnualEventId = annualeventid, CountyCode = countycode, ArchiveId = archiveid, SubjctId = _subjectid, Date = DateTime.Now.Date };

            var archives = iabl.GetArchives(annualeventid, archiveid, _subjectid, countycode);//db.Archives.Include("AnnualEvent").Include("Subject").Include("County").Include("Teacher");
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(annualeventid);
            ViewBag.AnnualEventId = annualeventid;
            ViewBag.Archives = archives.ToList();
            ViewBag.CountyCode = new SelectList(db.Counties.ToList(), "Code", "Name", countycode);
            ViewBag.SubjectName = db.Subjects.SingleOrDefault(s => s.Id == _subjectid).Name;
            return View(aam);
        }

        public ActionResult Score(string id,string returnaction)//the id is archive id
        {
            ExpertJudge ej = db.ExpertJudges.SingleOrDefault(e => e.SerialNumber == User.Identity.Name);

            if (string.IsNullOrEmpty(ej.ContactAddress) && string.IsNullOrEmpty(ej.ZipCode))
            {
                return RedirectToAction("SetContactInfo", "PW");
            }

            if (string.IsNullOrEmpty(id))
            {
                ModelState.AddModelError("id", "请填写参赛编号");
                //ViewBag.ErrorMsg = "请填写参赛编号";
            }
            id = id.ToUpper().Trim();
            if (ModelState.IsValid)
            {
                return RedirectToAction("Create", "Score", new { archiveid = id, returnaction = returnaction });
            }

            return RedirectToAction(returnaction, new { msg = "请填写参赛编号" });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
